package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.heima.common.constants.ArticleConstants.*;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    @Resource
    private ApArticleMapper apArticleMapper;
    /**
     * 加载文章列表
     * @param dto
     * @param type 1-加载更多 2-加载最新
     */
    public ResponseResult load(ArticleHomeDto dto, Short type){
        // 分页参数校验
        Integer size = dto.getSize();
        if(size == 0 || size == null){
            size = 10; // 默认为10
        }
        // 最大为50
        size = Math.min(size,50);

        // 类型校验
        if(!type.equals(ArticleConstants.LOADTYPE_LOAD_MORE) || !type.equals(ArticleConstants.LOADTYPE_LOAD_MORE)){
            type = ArticleConstants.LOADTYPE_LOAD_MORE;
        }
        // 频道参数校验
        if(StringUtils.isBlank(dto.getTag())) dto.setTag(DEFAULT_TAG);
        // 时间参数校验
        if(dto.getMinBehotTime() == null) dto.setMinBehotTime(new Date());
        if(dto.getMaxBehotTime() == null) dto.setMaxBehotTime(new Date());

        List<ApArticle> articles = apArticleMapper.loadArticleList(dto, type);

        return ResponseResult.okResult(articles);
    }
    @Resource
    private ApArticleConfigMapper apArticleConfigMapper;
    @Resource
    private ApArticleContentMapper apArticleContentMapper;
    @Resource
    private ArticleFreemarkerService articleFreemarkerService;


    /**
     * 保存app端文章
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        // 测试服务降级
        try {
            Thread.sleep(3000); // 睡眠3s（大于降级配置的2s）
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 1.参数校验
        if(dto == null){
            return ResponseResult.okResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApArticle articleInfo = new ApArticle();
        BeanUtils.copyProperties(dto,articleInfo);

        // 2.id是否存在

        // 2.1 id不存在，保存文章信息，文章内容，文章配置
        if(dto.getId() == null){
            // 保存文章信息
            this.save(articleInfo);
            // 保存文章内容
            ApArticleContent content = new ApArticleContent();
            content.setArticleId(articleInfo.getId());
            content.setContent(dto.getContent());
            apArticleContentMapper.insert(content);

            // 保存文章配置
            ApArticleConfig config = new ApArticleConfig(dto.getId());
            apArticleConfigMapper.insert(config);

        }else {// 2.2 id存在，修改文章信息，文章内容
            // 修改文章信息
            this.updateById(articleInfo);
            // 修改文章内容
            ApArticleContent articleContent = apArticleContentMapper.selectOne(new LambdaQueryWrapper<ApArticleContent>().eq(ApArticleContent::getArticleId, dto.getId()));
            articleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(articleContent);
        }
        // 异步调用，生成静态文件上传到minio中
        articleFreemarkerService.buildArticleToMinIo(articleInfo, dto.getContent());
        return ResponseResult.okResult(articleInfo.getId());
    }
}
